<template>
  <div class="container">
    <img src="@/assets/img/kc.png" alt="">
    <div class="search">

      <span style="margin-right:5px;">课程类型</span>
      <el-input v-model="searchForm.name" placeholder="驾照类型" style="width:300px;margin-right:10px;"></el-input>
      <span style="margin-right:5px;">教练</span>
      <el-input v-model="searchForm.coach" placeholder="驾照类型" style="width:300px;margin-right:10px;"></el-input>
      <el-button type="primary" @click="search" style="height:40px;" icon="el-icon-search">搜索</el-button>
  

    </div>
    <div class="list">
     <div class="wrapper" v-for="(item,index) in classList" :key="index" @click="getClassInfo(item.id)">
      <div class="little">
        <img :src="item.img" alt="">
        <div >{{item.name}}</div>
      </div>
     </div>
    
     <el-pagination class="fenye"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[4, 6, 8, 10]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    </div>
    <el-dialog title="课程信息" :visible.sync="infoStatus" width="50%">

      <el-form :model="info">
        <el-form-item style="width: 600px" label="图片">
          <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
          >
            <img v-if="info.img" :src="info.img" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item disabled label="课程名">
          <el-input disabled v-model="info.name" placeholder="请输入课程名"></el-input>
        </el-form-item>
        <el-form-item  label="描述">
          <el-input disabled type="textarea" autosize v-model="info.content" placeholder="请输入课程名"></el-input>
        </el-form-item>
        <el-form-item  label="教练">
          <el-input disabled v-model="info.coach" placeholder="请输入课程名"></el-input>
        </el-form-item>
        <el-form-item v-show="isApplica"  label="学员姓名">
          <el-input disabled   v-model="info.nickname" placeholder="请输入课程名"></el-input>
        </el-form-item>
        <el-form-item  v-show="isApplica" label="地址">
          <el-input v-model="info.adress" placeholder="请输入课程名"></el-input>
        </el-form-item>
       
        <el-form-item v-show="isApplica" label="时间">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="info.time"
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="infoStatus = false,btnStatus=true">取 消</el-button>
        <el-button @click="isApplica = true ,btnStatus=false" type="warning" v-show="btnStatus">预约</el-button>
        <el-button type="primary" @click="practice">提交</el-button>
      </span>
    </el-dialog>
  </div>
  
</template>

<script>
import {classInfo} from '@/api/class'
import { classList,practiceCar,searchClass } from "@/api/class";
export default {
    name:'Class',
    data(){
      return{
          classList:[],
          type:'',
          total: 0,
          licenseList: [],
          pageNumber: 1, //当前页
          pagesize: 4, //分页的数据
          infoStatus:false,
          isApplica:false,
          info:{},
          btnStatus:true,
          searchForm:{
            name:null,
            coach:null
          }
         

      }
    },
    methods:{
      handleSizeChange(val) {
      this.pagesize = val;
      this.getClassList()
    },
    handleCurrentChange(val) {
      this.pageNumber = val;
      this.getClassList()
    },
    async getClassList() {
      let res = await classList({
        pagesize: this.pagesize,
        pageNumber: this.pageNumber,
      });
      
      if (res.code == 200) {
        this.classList = res.data;
        this.total = res.totalCount;
      }
    },
    async getClassInfo(id){
    this.infoStatus = true
    let res =await classInfo(id)
    if(res.code==200){
      this.info = res.data
    }
    let infomation = JSON.parse(localStorage.getItem('infomation'))
    this.info.nickname = infomation.nickname
   },
  //  预约练车
   async practice() {
      let res = await practiceCar(this.info);
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success",
        });
      }
      this.btnStatus = true
     this.isApplica = false
     this.infoStatus = false
    },
    async search() {
      if (this.searchForm.name == "") this.searchForm.name = null;
      if (this.searchForm.coach == "") this.searchForm.coach = null;
     
      if (this.searchForm.name == null && this.searchForm.coach==null){
        this.getClassList()
        return
      }
     
      let res = await searchClass(this.searchForm);
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success",
        });
        this.classList = res.data;
        this.total = res.data.length;
      }
     
     
    },
    },
    mounted() {
      this.getClassList()
    },
}
</script>

<style scoped lang="scss">
.container{
  width: 87%;
  box-shadow: 4px 4px 15px rgb(136, 116, 116);
  margin-left: 40px;
  padding: 20px 0;
  box-sizing: border-box;
  img{
    width: 100%;
  }
  .search{
    background-color: #999d9c;
    width: 100%;
    height: 60px;
    display: flex;
    line-height: 60px;
    padding-left: 20%;
    color: #ffffff;
    align-items: center;
    
  }
  .list{
    position: relative;
    width: 100%;
    height: 600px;
    padding-left: 10px;
    padding-top: 10px;
    padding-right: 10px;
   
    display: flex;
    
    flex-wrap: wrap;
    .fenye{
      position: absolute;
      bottom: -10px;
      left: 30%;
    }
    .wrapper{
      width: 250px;
      height: 250px;
      border: solid 5px #999d9c;
      border-radius: 10px;
      margin-left: 30px;
     
      .little{
        width: 90%;
        height: 90%;
        background-color: #999d9c;
        margin-top: 5%;
        margin-left: 5%;
        img{
          width: 100%;
          height: 80%;
        }
        div{
          text-align: center;
          line-height: 30px;
        }
       
        
        
      }
      
    }
  }

}
</style>